﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Resources;
using Architect.BLL;
using Architect.CL;
using Architect.DAL;
using Architect.Web.UI;
using Architect.BO;
using Architect.BO.Util;

namespace ArchitectSite.Admin.Article
{
    public partial class CreateArticle : BasePage
    {
        #region Page event
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                PreUI();
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            bool result = false;
            if (validation()) return;

            Architect.BLL.Article article = CreateNew();
            article.Title = dataTitle.Text;
            article.CreatedDate = DateTime.Now;
            article.UpdatedDate = DateTime.Now;
            result = BoFactory.ArticleBO.Save(article);
            if (result) Response.Redirect("Default.aspx");
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListArticle.aspx");
        }
        protected void dataCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            dataMenuId.DataSource = GetMenuByCategory(int.Parse(dataCategory.SelectedValue.ToString()));
            dataMenuId.DataBind();
        }
        #endregion

        #region method
        public void PreUI()
        {
            dataCategory.DataSource = GetListCategory();
            dataCategory.DataBind();
            if (dataCategory.SelectedIndex != -1)
            {
                dataMenuId.DataSource = GetMenuByCategory(int.Parse(dataCategory.SelectedValue.ToString()));
                dataMenuId.DataBind();
            }
        }
        public bool validation()
        {
            return string.IsNullOrEmpty(dataTitle.Text);
        }
        public Architect.BLL.Article CreateNew()
        {
            DateTime current = DateTime.Now;
            Architect.BLL.Article article = new Architect.BLL.Article
            {
                Title = dataTitle.Text,
                MenuId = int.Parse(dataMenuId.SelectedValue.ToString()),
                Summary = Server.HtmlEncode(dataSummary.Text),
                Description = dataDescription.Text,
                Alias = BoCommon.RemoveVietnameseSigns(dataTitle.Text),
                OrderIndex = int.Parse(dataOrderIndex.Text),
                Thumbnail = dataThumbnail.Text,
                IsPublished = dataIsPublished.Checked,
                IsHot = IsHot.Checked,
                UpdatedDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                PublishedDate = DateTime.Parse(dataPublishedDate.Text)
            };
           return article;
        }
        public TList<Category> GetListCategory()
        {
            return DataRepository.CategoryProvider.GetAll();
        }
        public TList<Architect.BLL.Menu> GetMenuByCategory(int CategoryId)
        {
            return DataRepository.MenuProvider.GetByMenuType(CategoryId);               
        }
        #endregion

        
    }
}